/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author WangShuai
 */
public class DateUtil {

    /**
     * 解析日期字符串
     * @param date
     * @param formatStr 日期格式
     * @return
     */
    public static Date parseDate(String dateStr, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            return format.parse(dateStr);
        } catch (ParseException ex) {
            Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
     /**
     * 判断是否润年
     */
    public static boolean isLeapYear(String dateStr, String formatStr) {
        Date date = parseDate(dateStr, formatStr);
        return isLeapYear(date);
    }

     /**
     * 判断是否润年
     */
    public static boolean isLeapYear(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.isLeapYear(gc.get(Calendar.YEAR));
    }

      /**
     * 获取一个月的最后一天
     * @param dat
     * @return
     */
    public static String getEndDateOfMonth(String dateStr,String formatStr) {
        String result = null;
        Date date = parseDate(dateStr, formatStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //Calendar.MONTH为0-11
        int month = calendar.get(Calendar.MONTH)+1;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            result  = "31";
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            result  = "30";
        } else {
            if (isLeapYear(date)) {
                result  = "29";
            } else {
                result = "28";
            }
        }
        return result;
    }


    /**
     * 计算相差天数（向上舍入）
     * @param beginDateStr
     * @param endDateStr
     * @param formatStr
     * @return
     * @throws Exception
     */
    public static long spaceDay(String beginDateStr, String endDateStr, String formatStr) throws Exception {
        Date beginDate = parseDate(beginDateStr, formatStr);
        Date endDate = parseDate(endDateStr, formatStr);

        Calendar beginCld = Calendar.getInstance();
        Calendar endCld = Calendar.getInstance();

        beginCld.setTime(beginDate);
        endCld.setTime(endDate);
        //设置日期为0点整
        beginCld.set(Calendar.HOUR_OF_DAY, 0);
        beginCld.set(Calendar.MINUTE, 0);
        beginCld.set(Calendar.SECOND, 0);
        beginCld.set(Calendar.MILLISECOND, 0);

        endCld.set(Calendar.HOUR_OF_DAY, 0);
        endCld.set(Calendar.MINUTE, 0);
        endCld.set(Calendar.SECOND, 0);
        endCld.set(Calendar.MILLISECOND, 0);
        
        long result = (endCld.getTimeInMillis() - beginCld.getTimeInMillis()) / (24 * 60 * 60 * 1000);
        return result;
    }

    /**
     * 判断两个时间是否在同一个周
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeekDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (0 == subYear) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
            // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 比较时间大小
     * @param date1
     * @param date2
     * @return  -1: date1<date2   1:date1>date2   0:date1=date2
     */
    public static int compare(Date date1, Date date2) {
        if(date1.before(date2)){
            return -1;
        }
        if(date1.after(date2)) {
            return 1;
        }
        return 0;
    }

    public static void main(String[] args) {
        try {
            //System.out.println(DateUtil.getEndDateOfMonth("2008-2-10","yyyy-MM-dd"));
            System.out.println(compare(new Date(),parseDate("2010-11-04", "yyyy-MM-dd")));
        } catch (Exception ex) {
            Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
